﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Surpass.Model.Dto.ResponseDto.ResponseModel.ResultModel;
using Surpass.Model.SysDomain;
using Surpass.Repository.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Surpass.Admin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeptController : ControllerBase
    {
        private readonly ISysDeptRepository _sysDeptRepository;
        public DeptController(ISysDeptRepository sysDeptRepository)
        {
            _sysDeptRepository = sysDeptRepository;
        }
        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("depttree")]
        public IActionResult GetDeptTree()
        {
            var result = new ResponseModel().Succeed(_sysDeptRepository.GetDeptAll());
            return Ok(result);
        }
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="sysDept"></param>
        /// <returns></returns>
        [HttpPost,Route("adddept")]
        public IActionResult AddDept(SysDept sysDept)
        {
            _sysDeptRepository.Add(sysDept);
            var result = new ResponseModel().Succeed(_sysDeptRepository.SaveChanges());
            return Ok(result);
        }
        /// <summary>
        /// 详细
        /// </summary>
        /// <param name="did"></param>
        /// <returns></returns>
        [HttpGet, Route("findbyid")]
        public IActionResult FindById(int did)
        {
            return  Ok(new ResponseModel().Succeed(_sysDeptRepository.Find<SysDept>(did)));
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="sysDept"></param>
        /// <returns></returns>
        [HttpPost, Route("editdept")]
        public IActionResult EditDept(SysDept sysDept)
        {
            _sysDeptRepository.Update(sysDept);
            var result = new ResponseModel().Succeed(_sysDeptRepository.SaveChanges());
            return Ok(result);
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="did"></param>
        /// <returns></returns>
        [HttpPost, Route("deletedept")]
        public IActionResult DeleteDept(int did)
        {
            _sysDeptRepository.Delete<SysDept>(did);
            var result = new ResponseModel().Succeed(_sysDeptRepository.SaveChanges());
            return Ok(result);

        }
    }
}
